package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Super_type_dao;
import com.clinic.domain.Super_type;
import com.clinic.util.Util_imp;

public class Super_type_imp implements Super_type_dao {

	SessionFactory session_factory = null;

	public Super_type_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_super_type(Super_type se) {
		System.out.println("Enter - create_super_type");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int se_obj_id = 0;
		try {
			tx = session.beginTransaction();
			se_obj_id = (Integer) session.save(se);
			tx.commit();
			System.out.println("New Super_type Created with ID: " + se_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Super_type Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_super_type");
		return se_obj_id;
	}

	@Override
	public void update_super_type(Super_type se) {
		System.out.println("Enter - update_super_type");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(se);
			tx.commit();
			System.out.println("Super_type Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Super_type Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_super_type");
	}

	@Override
	public void delete_super_type(int se_obj_id) {
		System.out.println("Enter - delete_super_type");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Super_type se = (Super_type) session.get(Super_type.class, se_obj_id);
			session.delete(se);
			tx.commit();
			System.out.println("Super_type with ID: " + se_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Super_type Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_super_type");
	}

	@Override
	public Super_type get_super_type(int se_obj_id) {
		System.out.println("Enter - get_super_type");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Super_type se = null;
		try {
			tx = session.beginTransaction();
			se = (Super_type) session.createQuery("from Super_type where se_obj_id=:id")
					.setParameter("id", se_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Owner Name: " + se.getObj_owner_name());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_super_type");
		return se;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Super_type> get_all_super_types() {
		System.out.println("Enter - get_all_super_types");
		Session session = session_factory.openSession();
		List<Super_type> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Super_type").list();
			tx.commit();
			System.out.println("Number of Super_types returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_super_types");
		return l;
	}

	@Override
	public Super_type persistOrMerge(Super_type se) {
		return (Super_type) session_factory.getCurrentSession().merge(se);
	}

	@Override
	public int getsize() {
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Super_type").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Super_types: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}